package 参控股;

import Moudle.CanKongGu;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Main {
    static final String HTML_FOLDER = "D:\\测试临时文件\\2020年年报HTML";
    static final String INPUT_EXCEL = "D:\\测试临时文件\\参控股.xlsx";
    static final String OUTPUT_EXCEL = "D:\\测试临时文件\\参控股_检查完成.xlsx";

    public static void main(String[] args) throws IOException, InvalidFormatException {
        List<String> allHTMLPath = Utils.getAllFilePath(HTML_FOLDER);
        Map<String, List<String>> standardMap = transformToMap(allHTMLPath);
        List<CanKongGu> excelData = getExcelData(INPUT_EXCEL);
        estimateIsExist(standardMap, excelData);
        writeToExcel(excelData);
    }

    public static Map<String, List<String>> transformToMap(List<String> allFilePath){
        /**
         * 根据文件名
         * 以股票代码为key  文件路径为val   组成map
         */
        Map<String, List<String>> map = new LinkedHashMap<>();
        allFilePath.forEach(path -> {
            String fileName = path.substring(path.lastIndexOf("\\")+1);
            String stockCode = fileName.substring(0,fileName.indexOf("_"));
            if (map.containsKey(stockCode))
                map.get(stockCode).add(path);
            else{
                List<String> pathList = new ArrayList<>();
                pathList.add(path);
                map.put(stockCode, pathList);
            }
        });

        return map;
    }

    public static List<CanKongGu> getExcelData(String filePath) throws IOException, InvalidFormatException {
        /**
         * 从excel中读取数据
         * 组成list
         */
        Workbook workbook = WorkbookFactory.create(new File(filePath));
        Sheet sheet = workbook.getSheetAt(0);

        int rowSize = sheet.getPhysicalNumberOfRows();
        List<CanKongGu> list = new ArrayList<>();
        for (int i = 1; i < rowSize ; i++) {
            Row row = sheet.getRow(i);
            list.add(new CanKongGu(row.getCell(0)+"", row.getCell(1)+""));
        }
        return list;
    }

    public static void estimateIsExist(Map<String, List<String>> standardMap, List<CanKongGu> excelData){
        /**
         * 判断公司名称是否在html中
         */
        excelData.forEach(canKongGu -> {
            canKongGu.setExist(false);
            if (standardMap.containsKey(canKongGu.secCode)){
                standardMap.get(canKongGu.secCode).forEach(htmlPath -> {
                    try {
                       Document doc = Jsoup.parse(new File(htmlPath), "UTF-8", "http://example.com/");
                       if (doc.text().contains(canKongGu.companyName))
                           canKongGu.setExist(true);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                });
            }

            if (canKongGu.isExist())
                System.out.println(canKongGu.companyName + "   存在");
            else
                System.out.println(canKongGu.companyName + "  不存在");

        });
    }

    public static void writeToExcel(List<CanKongGu> dataList) throws IOException {

        System.out.println("开始写入excel...");
        Workbook workbook = new SXSSFWorkbook();
        Sheet sheet = workbook.createSheet();

        sheet.createRow(0).createCell(0).setCellValue("股票代码");
        sheet.createRow(0).createCell(1).setCellValue("被参控公司");
        sheet.createRow(0).createCell(2).setCellValue("年报是否存在");

        int cellIndex = 1;
        for (CanKongGu canKongGu : dataList){
            Row row = sheet.createRow(cellIndex++);
            row.createCell(0).setCellValue(canKongGu.secCode);
            row.createCell(1).setCellValue(canKongGu.companyName);

            String isExist = "不存在";
            if (canKongGu.isExist())
                isExist = "存在";

            row.createCell(2).setCellValue(isExist);
        }

        workbook.write(new FileOutputStream(OUTPUT_EXCEL));
    }
}
